<?php

namespace App\Http\Controllers\Api\Image;


use App\Services\OSS;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;


class FilesController extends Controller
{
    public function store(Request $request)
    {
        $user = $request->user();
        $prefix = $user? $user->id : 'mobile';
        $file = $request->file('files');
        if($request->hasFile('files')){
            $original_name = $file->getClientOriginalName();
            $filePath = $file->getRealPath();
            $extension = $file->extension();
            $realType = $file->getMimeType();
            $file_name = 'uploads/files/'.date("Y/m/d", time())."/$prefix".'_'.Str::random(random_int(10,20)) . '.'.$extension;
            if (OSS::publicUpload(config('aliyunoss.BucketName'), $file_name, $filePath,[
                'ContentType' => $realType,
            ])){
                return response()->json([
                    'code' => 0,
                    'msg' => '图片上传成功',
                    'src' => config('aliyunoss.ALIOSS_URL').'/'.$file_name,
                    'name' => $original_name
                ]);
            }
        }
        return response()->json([
            'code' => 1,
            'msg' => '图片上传失败',
        ]);
    }
}
